// 记录日志记录配置文件
const log4js = require('log4js');
const path = require('path');

log4js.configure({
    appenders: {
        sql: {
            // 定义一个sql日志出口
            // type: "file",
            type: "dateFile",
            filename: path.resolve(__dirname, "logs", "sql", "logger.log"),
            maxLogSize: 1024 * 1024, //配置文件的字节数
            keepFileExt: true,
            daysToKeep: 3, //保留三天的日志记录
            layout: {
                type: "pattern",
                pattern: "%d{yyyy-MM-dd hh:mm:ss} %p %c:%m%n"
            }
        },
        default: {
            type: 'stdout', //默认的日志放到控制台输出
            filename: path.resolve(__dirname, "logs", "sql", "logger.log")
        },
        api: {
            // 定义一个api日志出口
            // type: "file",
            type: "dateFile",
            filename: path.resolve(__dirname, "logs", "api", "logger.log"),
            maxLogSize: 1024 * 1024, //配置文件的字节数
            keepFileExt: true,
            daysToKeep: 3, //保留三天的日志记录
            layout: {
                type: "pattern",
                pattern: "%d{yyyy-MM-dd hh:mm:ss} %p %c:%m%n"
            }
        },
    },
    categories: {
        sql: {
            appenders: ['sql'],//该分类使用出口sql的配置写入日志
            level: 'all'
        },
        default: {
            appenders: ["default"],
            level: "all"
        },
        api: {
            appenders: ['api'],//该分类使用出口sql的配置写入日志
            level: 'all'
        },
    }
})

process.on("exit", () => {
    log4js.shutdown();
})

exports.sqlLogger = log4js.getLogger("sql");
exports.logger = log4js.getLogger();
exports.apiLogger = log4js.getLogger('api');